package com.zhihaoliang.map.details

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import com.zhihaoliang.map.data.DestinationsRepository
import com.zhihaoliang.map.data.ExploreModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

import com.zhihaoliang.map.base.Result

/**
 * @author 支豪亮
 * @date 2023/1/16 16:55
 * @des DetailsActivity 的 viewModle
 */
@HiltViewModel
class DetailsViewModel @Inject constructor(
    private val destinationsRepository: DestinationsRepository,
    savedStateHandle: SavedStateHandle
):ViewModel() {

    private val cityName = savedStateHandle.get<String>(KEY_ARG_DETAILS_CITY_NAME)!!

    val cityDetails:Result<ExploreModel>
    get() {
        val destination = destinationsRepository.getDestination(cityName)
        return if(destination != null) Result.Success(destination) else Result.Error(IllegalArgumentException("City doesn't exist"))
    }
}